package com.isczy.eduservice.controller;


import com.isczy.eduservice.entity.EduVideo;
import com.isczy.eduservice.service.EduVideoService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author CZY
 * @since 2021-01-02
 */
@Api(description = "课程视频 前端控制器")
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    /***
     * ***************************************
     * 功能描述:添加小节
     * @param eduVideo
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:28】
     * @author modify:
     */
    @PostMapping("addVideo")
    public DataResult addVideo(@RequestBody EduVideo eduVideo) {
        try {
            videoService.save(eduVideo);
            return DataResult.builder()
                    .message("添加小节成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("添加小节失败")
                    .params(eduVideo)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据id查询小节
     * @param videoId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-06 11:01】
     * @author modify:
     */
    @ApiOperation(value = "根据id查询小节")
    @GetMapping("getVideoInfo/{videoId}")
    public DataResult getChapterInfo(@PathVariable String videoId) {
        try {
            return DataResult.builder()
                    .data(videoService.getById(videoId))
                    .message("根据id查询小节成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据id查询小节失败")
                    .params(videoId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改小节
     * @param eduVideo
     * @return {@link com.isczy.utils.DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-06 11:02】
     * @author modify:
     */
    @ApiOperation(value = "修改小节")
    @PostMapping("updateVideo")
    public DataResult updateVideo(@RequestBody EduVideo eduVideo) {
        try {
            videoService.updateById(eduVideo);
            return DataResult.builder()
                    .message("修改小节成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("修改小节失败")
                    .params(eduVideo)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:删除小节
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:28】
     * @author modify:
     */
    @DeleteMapping("{id}")
    public DataResult deleteVideo(@PathVariable String id) {
        try {
            videoService.deleteVideoById(id);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除小节失败")
                    .params(id)
                    .build();
        }
    }

}

